<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>改变文件的所有者</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.chmod.html">chmod</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.clearstatcache.html">clearstatcache</a></div>
 <div class="up"><a href="ref.filesystem.html">Filesystem 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.chown" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">chown</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">chown</span> &mdash; <span class="dc-title">改变文件的所有者</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.chown-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>chown</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$user</code></span>
   )</div>

  <p class="para rdfs-comment">
   尝试将文件 <em><code class="parameter">filename</code></em> 的所有者改成用户
   <em><code class="parameter">user</code></em>（由用户名或用户 ID 指定）。
   只有超级用户可以改变文件的所有者。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.chown-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">filename</code></em></dt>

     <dd>

      <p class="para">
       文件路径。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">user</code></em></dt>

     <dd>

      <p class="para">
       用户名或数字。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.chown-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.chown-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2285">
    <p><strong>Example #1 简单的 <span class="function"><strong>chown()</strong></span> 用法</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;File&nbsp;name&nbsp;and&nbsp;username&nbsp;to&nbsp;use<br /></span><span style="color: #0000BB">$file_name</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"foo.php"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$path&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"/home/sites/php.net/public_html/sandbox/"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$file_name&nbsp;</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$user_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"root"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Set&nbsp;the&nbsp;user<br /></span><span style="color: #0000BB">chown</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user_name</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Check&nbsp;the&nbsp;result<br /></span><span style="color: #0000BB">$stat&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stat</span><span style="color: #007700">(</span><span style="color: #0000BB">$path</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">posix_getpwuid</span><span style="color: #007700">(</span><span style="color: #0000BB">$stat</span><span style="color: #007700">[</span><span style="color: #DD0000">'uid'</span><span style="color: #007700">]));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [name] =&gt; root
    [passwd] =&gt; x
    [uid] =&gt; 0
    [gid] =&gt; 0
    [gecos] =&gt; root
    [dir] =&gt; /root
    [shell] =&gt; /bin/bash
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.chown-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">此函数不能作用于<a href="features.remote-files.html" class="link">远程文件</a>，被检查的文件必须是可通过服务器的文件系统访问的。</span></p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">当启用
<a href="features.safe-mode.html" class="link">安全模式</a>时，
PHP 会检查被操作的文件或目录是否与被执行的脚本有相同的 UID（所有者）。</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.chown-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.chmod.html" class="function" rel="rdfs-seeAlso">chmod()</a> - 改变文件模式</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.chmod.html">chmod</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.clearstatcache.html">clearstatcache</a></div>
 <div class="up"><a href="ref.filesystem.html">Filesystem 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
